<?php
namespace App\Console\Commands;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class UserCreditData extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'user:creditdata';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'create a users credit data';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        if ($this->confirm('Do you wish to continue? [y|N]')) {

            $userCredit = 0;
            //credit_edition
            $createData = [];
            for ($j=2010;$j<2019;++$j){

                for ($i = 1; $i < 13; ++$i) {
                    $createData['user_id'] = 33;
                    $createData['year'] = $j;
                    $createData['month'] = $i;
                    $createData['year_credit'] = 10 * $i;
                    $createData['month_credit'] = 10;
                    $createData['created_at']= Carbon::now();
                    $createData['category_id'] = 8;
                    $userCredit += $createData['year_credit'];
                    $createData['credit'] = $userCredit;
                    if ($i < Carbon::now()->month && $j < 2019){
                        //create or update
                        $credit = DB::table('credit_edition')->where(['user_id'=>33,'year'=>$j,'month'=>$i])->get();
                        if ($credit->isEmpty()){

                            DB::table('credit_edition')->insert($createData);
                        }
                    }
                }
            }
            $usersData = ['credit' => $userCredit, 'reward' => $userCredit];
            DB::table('users')->where(['id' => 33])->update($usersData);

            //user credit reward order
            $rewardLog = [];
            for ($k=1;$k<100;++$k){
                $rewardLog['user_id'] = 33;
                $rewardLog['params'] = rand(10,99);
                $rewardLog['created_by'] = 1;
                $rewardLog['type'] = rand(1,2);
                $rewardLog['app_id'] = 15;
                $rewardLog['vuld_id'] = rand(1,100);

                DB::table('reward_log')->insert($rewardLog);
            }
            //orders
            $orders = [];
            for ($k=1;$k<100;++$k){
                $orders['sn'] = 'SN'.date('YmdH:i:s',time()).rand(1000,9999);
                $orders['goods_id'] = rand(1,99);
                $orders['user_id'] = 33;
                $orders['consignee'] = 'zeronlee,陕西省西安市环普,15094001356';
                $orders['payed'] = 15;
                $orders['total'] = 15;
                $orders['num'] = 1;
                DB::table('orders')->insert($orders);
            }
            dd('success');
        }
    }
}
